package com.ywgt.controller;

import com.github.pagehelper.Page;
import com.ywgt.pojo.Region;
import com.ywgt.service.RegionService;
import com.ywgt.util.ResponseUtil;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/region")
@ApiModel("区域管理")
public class RegionController {

    @Autowired
    RegionService regionService;

    /**
     * page: 1,
     *limit: 20,
     *name: undefined,
     *code: undefined
     * @return
     */
    @GetMapping("/list")
    public Object getRegions(String name, Integer code,
                             @RequestParam(required = false,defaultValue = "1") int page,
                             @RequestParam(required = false,defaultValue = "10") int limit){
        Page<Region> regions=regionService.getRegions(name,code,page,limit);
        List<Region> items = regions.getResult();
        long total = regions.getTotal();
        Map<String,Object> data=new HashMap<>();
        data.put("total",total);
        data.put("items",items);
        return ResponseUtil.ok(data);
    }
}
